<template>
  <div>
    <el-container class="home_container">
      <!-- 頭部區域 -->
      <el-header>
        <div class="home_header_box">
          <img src="../assets/logo.png">
          <span>電商後台管理系統</span>
        </div>
        <el-button type="info"  @click="logout">退出</el-button>
      </el-header>

      <el-container>
        <!-- 側邊欄 -->
        <el-aside :width="isCollapse ? '64px': '200px'">
          <div class="toggle_btn" @click="isCollapse = !isCollapse">|||</div>
          <el-menu
            :default-active="activePath"
            background-color="#333744"
            text-color="#fff"
            class="el-menu-vertical-demo"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
           >
            <el-submenu :index="item.id + ''" v-for="(item) in leftMenuList" :key="item.id">
              <template slot="title">
                <i :class="lefListIcon[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
                <el-menu-item :index="'/'+ subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="defaultActiveState('/'+ subItem.path)">
                  <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主體區域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script>
export default {
  data () {
    return {
      leftMenuList: [], // 左側菜單欄數據
      lefListIcon: { // 左侧菜单栏icon图标数据
        125: 'el-icon-s-custom',
        103: 'el-icon-s-management',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      isCollapse: false, // 控制左侧菜单栏是否折叠
      activePath: '' // 当前激活的菜单
    }
  },
  created () {
    this.getLeftMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    logout () { // 退出功能
      sessionStorage.clear()
      this.$router.push('/login')
    },
    async getLeftMenuList () { // 獲取左側菜單欄數據
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg)
      this.leftMenuList = res.data
    },
    defaultActiveState (activePath) { // 激活的菜单栏
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }

  }
}
</script>

<style lang="less" scoped>

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .el-button {
    height: 40px;
    margin-right: 100px;
  }
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}
div {
  height: 100%;
  .home_container {
     height: 100%;
     .el-menu-vertical-demo {
       border: 0;
     }
  }

  .home_header_box {
    display: flex;
    align-items: center;
    img {
      height: 40px;
      width: 40px;
      vertical-align: middle;
    }

    span {
      margin-left: 20px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.toggle_btn {
  height: 30px;
  text-align: center;
  background-color: #484d5c;
  color: #fff;
}
</style>
